<?php

App::uses('AppModel', 'Model');

class KalkulatorKomunalna extends AppModel {
    
    public $useTable = 'kalkulator_komunalna';
    
    public function calculate($data) {
        $value = 0;
        
        switch($data['vrsta_vozila']) {
            case 'teretno':
                $queryValue = (float)$data['nosivost'];
                break;
                
            default:
                $queryValue = (float)$data['zapremina'];
                break;
        }
        
        $row = $this->find('first', array(
            'fields'     => array('value'),
            'conditions' => array(
                'vrsta_vozila' => $data['vrsta_vozila'],
                'min <'        => $queryValue,
                'max >='       => $queryValue
            )
        ));
        if ($row) {
            $value = (float)$row[$this->name]['value'];
        }
        
        if ($data['invalid']) {
            $value = 0;
        }
        
        return $value;
    }
    
}
